﻿CREATE PROCEDURE [dbo].[usp_insert_AlertCondition]
(
           @AlertConditionID uniqueidentifier
           ,@AlertID uniqueidentifier
           ,@LeftSide varchar(50)
           ,@RightSide varchar(512)
           ,@Operator varchar(4)
		   ,@Debug bit = 0
)
AS

SET NOCOUNT ON;

-- Bootstrap
DECLARE @DebugPrefix       varchar(10);
DECLARE @ErrorCode         int;

SET @ErrorCode          = 0;
SET @DebugPrefix        = '>>> DEBUG: ';

-- Print parameters
IF (@Debug > 0)
BEGIN
	PRINT @DebugPrefix + '@AlertConditionID = '  + CONVERT(varchar, @AlertConditionID);
	PRINT @DebugPrefix + '@AlertID = '           + CONVERT(varchar, @AlertID);
	PRINT @DebugPrefix + '@LeftSide = '			 + @LeftSide;
	PRINT @DebugPrefix + '@RightSide = '         + @RightSide;
	PRINT @DebugPrefix + '@Operator = '			 + @Operator;
END;

INSERT INTO [dbo].[AlertCondition]
           ([AlertConditionID]
           ,[AlertID]
           ,[LeftSide]
           ,[RightSide]
           ,[Operator])
     VALUES(
           @AlertConditionID
           ,@AlertID
           ,@LeftSide
           ,@RightSide
           ,@Operator)

-- CHECK FOR ERROR
SET @ErrorCode = @@Error;

IF (@ErrorCode <> 0) GOTO ERROR_HANDLER;

RETURN 0;

-- Handles errors.
ERROR_HANDLER:
IF (@ErrorCode <> 0)
BEGIN
    IF (@Debug > 0) 
		BEGIN
			PRINT @DebugPrefix + 'Error Code = ' + CAST(@ErrorCode AS varchar(10)) + ' occurred.';
			RETURN @ErrorCode
		END

    -- Don't return any evidence of what specific error occurred, i.e. 
    -- we want to ward off potential hackers.
    RETURN 1;
END;

SET NOCOUNT OFF;
